package org.imlinker.core;

import java.awt.Frame;

import org.imlinker.gui.MainWindow;
import org.imlinker.gui.PreferenceWindow;
import org.imlinker.gui.TrayIconPopup;
import org.imlinker.gui.WindowSaver;
import org.imlinker.list.ListManager;
import org.imlinker.login.Login;
import org.imlinker.plugin.PluginManager;
import org.imlinker.setting.Settings; 

import org.jivesoftware.smack.XMPPConnection;

/**
 * 核心类，启动各部分资源，以及协调他们之间的配置.
 * 
 */
public class Core extends Thread {

	private static Settings settings;

	public static void deleteValue(String url) {
		if (settings != null) {
			settings.deleteValue(url);
		}
	}

	public static boolean getBooleanAt(String url) {
		if (settings == null) {
			return false;
		}
		return settings.getBooleanAt(url);
	}

	/**
	 * 获得用户配置文件 元素数量.
	 */
	public static int getElementSize(String url) {
		if (settings == null) {
			return 0;
		}
		return settings.getElementSize(url);
	}

	/**
	 * 获得用户配置文件 相关地址的字符串值.
	 */
	public static String getValue(final String url) {
		if (settings == null) {
			return null;
		}

		return settings.getValue(url);
	}

	/**
	 * 保存用户配置文件.
	 */
	public static void save() {
		settings.save();
	}

	/**
	 * 设置用户配置文件相关地址的值.
	 */
	public static void setValueAt(final String url, final String value) {
		if (settings == null) {
			return;
		}

		settings.setValueAt(url, value);
	}

	/**
	 * Core的构造函数，输入参数为程序的最上层目录，即imLinker.jar文件所在文件夹.
	 * 
	 * @param paraPath
	 *            程序最上层目录
	 */
	public Core(final String paraPath) {
		this.path = paraPath;

		// 给窗口位置保存和图片资源的两个静态类提供地址，使它们得以找到资源。
		WindowSaver.setPath(paraPath);
	}

	final public void run() {

		// 启动Login线程。
		Login login = new Login(path);
		login.start();

		// 等待登录成功
		try {
			login.join();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

		// 获得登录后的XMPPConneciton
		connection = login.getConnection();

		// 初始化个人设置
		String userWithResource = connection.getUser();
		settings = new Settings(path, userWithResource);

		// 初始化插件系统
		pluginManager = new PluginManager(connection, path);

		// 初始化列表管理器
		listManager = new ListManager(connection, pluginManager, this);

		// 初始化托盘图标
		popup = new TrayIconPopup(this);

		// 显示主界面
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				mainWindow = new MainWindow(Core.this, connection);
				mainWindow.setVisible(true);

				mainWindow.setListModel(listManager);
			}
		});
	}

	public void openFirstMessage() {
		if (!listManager.openFirstMessage()) {
			mainWindow.setVisible(!mainWindow.isVisible());
		}
	}

	public void createPreferenceWindow() {
		new PreferenceWindow(connection, pluginManager, mainWindow)
				.setVisible(true);
	}

	public void setMessageNum(final int number) {
		popup.setMessageNum(number);
		mainWindow.setMessageNum(number);
	}

	public void showMainWindow() {
		mainWindow.setVisible(true);
		mainWindow.setExtendedState(Frame.NORMAL);
	}

	private MainWindow mainWindow;

	private XMPPConnection connection;

	private String path;

	private ListManager listManager;

	private TrayIconPopup popup;

	private PluginManager pluginManager;

}
